package com.github.caijh.mall.store.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.caijh.commons.util.DateUtils;
import com.github.caijh.commons.util.Strings;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.common.CommonPage;
import com.github.caijh.mall.common.CommonResult;
import com.github.caijh.mall.common.PageParams;
import com.github.caijh.mall.store.model.StoreProductReply;
import com.github.caijh.mall.store.request.StoreProductReplyAddRequest;
import com.github.caijh.mall.store.request.StoreProductReplyCommentRequest;
import com.github.caijh.mall.store.request.StoreProductReplyRequest;
import com.github.caijh.mall.store.request.StoreProductReplySearchRequest;
import com.github.caijh.mall.store.response.StoreProductReplyResponse;
import com.github.caijh.mall.store.service.StoreProductReplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/admin/store/product/reply")
@Api(tags = "商品 -- 评论") //配合swagger使用
public class StoreProductReplyController extends BaseController {

    @Autowired
    private StoreProductReplyService storeProductReplyService;

    /**
     * 分页显示评论表
     *
     * @param request    搜索条件
     * @param pageParams 分页参数
     * @return page
     */
    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<StoreProductReplyResponse>> getList(
            @Validated StoreProductReplySearchRequest request,
            @Validated PageParams pageParams) {
        CommonPage<StoreProductReplyResponse> storeProductReplyCommonPage =
                CommonPage.restPage(storeProductReplyService.getList(request, pageParams));
        return CommonResult.success(storeProductReplyCommonPage);
    }

    /**
     * 新增评论表
     *
     * @param request 新增参数
     */
    @ApiOperation(value = "新增")
    @PostMapping(value = "/save")
    public void save(
            @RequestBody @Validated StoreProductReplyAddRequest request) {
        storeProductReplyService.virtualCreate(request);
    }

    /**
     * 删除评论表
     *
     * @param id 评论id
     */
    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete/{id}")
    public void delete(@PathVariable Integer id) {
        LambdaUpdateWrapper<StoreProductReply> lmdUp = new LambdaUpdateWrapper<>();
        lmdUp.set(StoreProductReply::getIsDel, 1);
        lmdUp.eq(StoreProductReply::getId, id);
        storeProductReplyService.update(lmdUp);
    }

    /**
     * 修改评论表
     *
     * @param storeProductReplyRequest 修改参数
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public void update(
            @RequestBody @Validated StoreProductReplyRequest storeProductReplyRequest) {
        StoreProductReply storeProductReply = new StoreProductReply();
        BeanUtils.copyProperties(storeProductReplyRequest, storeProductReply);
        storeProductReply.setId(storeProductReplyRequest.getId());
        storeProductReplyService.updateById(storeProductReply);
    }

    /**
     * 查询评论表信息
     *
     * @param id 评论id
     * @return StoreProductReply
     */
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info/{id}")
    public CommonResult<StoreProductReply> info(@PathVariable Integer id) {
        StoreProductReply storeProductReply = storeProductReplyService.getById(id);
        return CommonResult.success(storeProductReply);
    }

    /**
     * 回复商品评论
     *
     * @param request StoreProductReplyCommentRequest 回复参数
     */
    @ApiOperation(value = "回复")
    @PostMapping(value = "/comment")
    public void comment(@RequestBody StoreProductReplyCommentRequest request) {
        List<Integer> ids = Strings.toIntListByComma(request.getIds());
        for (Integer id : ids) {
            LambdaUpdateWrapper<StoreProductReply> lup = new LambdaUpdateWrapper<>();
            lup.eq(StoreProductReply::getId, id);
            lup.set(StoreProductReply::getMerchantReplyContent, request.getMerchantReplyContent());
            lup.set(StoreProductReply::getMerchantReplyTime, ((int) DateUtils.currentTimestamp()));
            lup.set(StoreProductReply::getIsReply, true);
            storeProductReplyService.update(lup);
        }
    }

}



